package com.tedu.checkIn.service.impl;

import com.tedu.checkIn.mapper.BedMapper;
import com.tedu.checkIn.mapper.ExcusedMapper;
import com.tedu.checkIn.mapper.RecordMapper;
import com.tedu.checkIn.pojo.dto.BedQuery;
import com.tedu.checkIn.pojo.dto.ExcusedParam;
import com.tedu.checkIn.pojo.dto.ExcusedQuery;
import com.tedu.checkIn.pojo.dto.RecordQuery;
import com.tedu.checkIn.pojo.entity.Excused;
import com.tedu.checkIn.pojo.vo.ExcusedVO;
import com.tedu.checkIn.service.ExcusedService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ExcusedServiceImpl implements ExcusedService {
    @Autowired
    ExcusedMapper excusedMapper;
    @Autowired
    RecordMapper recordMapper;
    @Autowired
    BedMapper bedMapper;

    @Override
    public List<ExcusedVO> select(ExcusedQuery excusedQuery) {
        log.debug("查询请假信息列表业务，参数={}",excusedQuery);
        List<ExcusedVO> list=excusedMapper.select(excusedQuery);
        for(ExcusedVO excusedVO:list){
            RecordQuery recordQuery=new RecordQuery();
            BedQuery bedQuery=new BedQuery();
            recordQuery.setUsername(excusedVO.getUsername());
            bedQuery.setId(recordMapper.select(recordQuery).get(0).getBedId());
            excusedVO.setBedName(bedMapper.selectBed(bedQuery).get(0).getName());
        }
        log.debug("查询请假信息列表业务，入数={}，出参={}",excusedQuery,list);
        return list;
    }

    @Override
    public void save(ExcusedParam excusedParam) {
        log.debug("保存请假信息业务，参数={}",excusedParam);
        Excused excused=new Excused();
        BeanUtils.copyProperties(excusedParam,excused);
        if(excused.getId()==null){
            excused.setCreateTime(new Date());
            excused.setStatus("10");
            excused.setReviewStatus("10");
            excusedMapper.insert(excused);
        }else {
            excused.setCreateTime(new Date());
            excusedMapper.update(excused);
        }
    }

    @Override
    public void delete(Integer id) {
        log.debug("删除请假信息业务，参数={}",id);
        excusedMapper.delete(id);
    }
}
